package com.xx.pmis.web.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xx.pmis.model.HotelExpense;
import com.xx.pmis.model.OtherExpense;
import com.xx.pmis.model.TourismPlan;
import com.xx.pmis.model.TravellingExpense;
import com.xx.pmis.services.HotelExpenseService;
import com.xx.pmis.services.OtherExpenseService;
import com.xx.pmis.services.TourismPlanService;
import com.xx.pmis.services.TravellingExpenseService;

/**
 * @class 旅游实际支出，该值是旅游交通费、住宿费、其他等费用总和 的实时值<br>
 * 
 * @editor 石星星 @Date 2011-9-28 上午10:48:13 <br>
 * @version jdk_1.5.0
 */
public class TourismBudget extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(TourismBudget.class);
	HotelExpenseService HotelExpenseervice = getPMISServices().getHotelExpenseService();
	OtherExpenseService otherExpenseService = getPMISServices().getOtherExpenseService();
	TravellingExpenseService travellingExpenseService = getPMISServices().getTravellingExpenseService();
	TourismPlanService tourismPlanService = getPMISServices().getTourismPlanService();

	AccountUtil accountUtil = new AccountUtil();

	public String sumOfTourismExpenses(TourismPlan tourismPlan) throws Exception {
		// 预算旅游计划花费，交通费和，旅游计划
		double totalMoney = 0.00;
		double totalOe = 0.00; // 其他费用总和
		double totalHe = 0.00; // 住宿费总和
		double totalTe = 0.00; // 交通费总和
		// 其他费用
		List<?> oeList = otherExpenseService.getAllOtherExpenseByCondition(" where tpID = '" + tourismPlan.getTpID()
				+ "'");
		Iterator<?> oeIt = oeList.iterator();
		while (oeIt.hasNext()) {
			OtherExpense oe = (OtherExpense) oeIt.next();
			totalOe = totalOe + Double.parseDouble(oe.getOeExpenses());
		}
		// 住宿费
		List<?> heList = HotelExpenseervice.getAllHotelExpenseByCondition(" where tpID = '" + tourismPlan.getTpID()
				+ "'");
		Iterator<?> heIt = heList.iterator();
		while (heIt.hasNext()) {
			HotelExpense he = (HotelExpense) heIt.next();
			totalHe = totalHe + Double.parseDouble(he.getHeExpenses()) * Double.parseDouble(he.getDayNumber());
		}
		// 交通费
		List<?> teList = travellingExpenseService.getAllTravellingExpenseByCondition(" where tpID = '"
				+ tourismPlan.getTpID() + "'");
		Iterator<?> teIt = teList.iterator();
		while (teIt.hasNext()) {
			TravellingExpense te = (TravellingExpense) teIt.next();
			totalTe = totalTe + Double.parseDouble(te.getTeExpenses()) * Double.parseDouble(te.getTeNumber());
		}

		// 计算以上的和
		totalMoney = totalOe + totalHe + totalTe;
		// 把Double转化成String型
		String total = String.valueOf(totalMoney);
		return total;
	}

	/**
	 * 数据分析
	 * 
	 * @return
	 */
	public List<TourismPlan> getTourismPlanList() {
		List<TourismPlan> tourismPlanList = new ArrayList<TourismPlan>();
		// 旅游
		tourismPlanList.add(getTourismPlanByType("0"));
		// 出差
		tourismPlanList.add(getTourismPlanByType("1"));
		// 探亲
		tourismPlanList.add(getTourismPlanByType("2"));
		return tourismPlanList;
	}

	/**
	 * 根据类型（0，1，2）查询近五年的消费额和消费次数
	 * 
	 * @param type
	 * @return
	 */
	public TourismPlan getTourismPlanByType(String type) {
		TourismPlan tp = new TourismPlan();
		String year = CurrentDate.currentYear();
		int yearInt = Integer.parseInt(year);
		String[] str1 = getTourismPlanByYearAndType(year, type);
		tp.setTotalEstimate1(str1[0]);
		tp.setTotalDays1(str1[1]);
		String[] str2 = getTourismPlanByYearAndType(String.valueOf(yearInt - 1), type);
		tp.setTotalEstimate2(str2[0]);
		tp.setTotalDays2(str2[1]);
		String[] str3 = getTourismPlanByYearAndType(String.valueOf(yearInt - 2), type);
		tp.setTotalEstimate3(str3[0]);
		tp.setTotalDays3(str3[1]);
		String[] str4 = getTourismPlanByYearAndType(String.valueOf(yearInt - 3), type);
		tp.setTotalEstimate4(str4[0]);
		tp.setTotalDays4(str4[1]);
		String[] str5 = getTourismPlanByYearAndType(String.valueOf(yearInt - 4), type);
		tp.setTotalEstimate5(str5[0]);
		tp.setTotalDays5(str5[1]);

		tp.setTypeName(getTypeNamebyTpType(type));
		return tp;
	}

	/**
	 * 根据某一类型查询一年的消费额和消费次数
	 * 
	 * @param year
	 * @param type
	 * @return
	 */
	public String[] getTourismPlanByYearAndType(String year, String type) {
		// str[0] 一年内消费总额；str[1] 一年内消费总次数
		String[] str = new String[2];
		List<TourismPlan> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService.getAllTourismPlanByCondition(" where year(beginDate) = '" + year
					+ "' and tpType='" + type + "'");
		} catch (Exception e) {
			log.info("【旅游管理/旅游计划】查询所有【旅游计划】异常！" + e.toString());
		}
		// 一年消费总金额
		double totalEstimate = 0.00;
		// 一年内消费次数
		int totalDays = 0;
		// 实时汇总该旅游计划的消费总额
		Iterator<?> it = tourismPlanList.iterator();
		while (it.hasNext()) {
			TourismPlan tourismPlan = (TourismPlan) it.next();
			// 一次计划的总费用
			String totalMoney = "";
			try {
				totalMoney = sumOfTourismExpenses(tourismPlan);
			} catch (Exception e) {
				log.info("【旅游管理/旅游计划】计算【旅游计划】实际支出异常！" + e.toString());
			}

			totalEstimate = totalEstimate + Double.parseDouble(totalMoney);
			str[0] = accountUtil.if1ZeroTo2Zero(new DecimalFormat("####.##").format(totalEstimate));
			totalDays = totalDays + Integer.parseInt(tourismPlan.getTpDays());
			str[1] = String.valueOf(totalDays);

		}

		return str;
	}

	/**
	 * 类型转换
	 * 
	 * @param type
	 * @return
	 */
	public String getTypeNamebyTpType(String type) {
		String typeName = "";
		if (type.equals("0")) {
			typeName = "旅游";
		} else if (type.equals("1")) {
			typeName = "出差";
		} else if (type.equals("2")) {
			typeName = "探亲";
		} else {
			log.info("输入类型" + type + "值不正确，请核对后输入！（0,1,2分别代表旅游、出差、探亲）");
		}
		return typeName;
	}

}
